package com.fdy;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.GeneralBasicOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.GeneralBasicOCRResponse;

import java.util.List;

/**
 * <p>描述：</p>
 *
 * @author fangdy
 * @create 2023-12-11 12:46
 */
public class DescribeInstances {
    public static void main(String[] args) {
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取

            List<String> list = FileUtil.readLines("D:\\zl\\ocr\\tencent_ocr.txt", CharsetUtil.CHARSET_UTF_8);
            if (CollUtil.isEmpty(list)) {
                System.out.println("请配置ocr信息");
                return;

            }
            Credential cred = new Credential(list.get(0).trim(), list.get(1).trim());            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-shanghai", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();

            req.setImageUrl("https://10.idqqimg.com/qqcourse_logo_ng/ajNVdqHZLLBFstZ2icCLKlbkicTicoJzGnMBFY3iaOicRGA9BRIGaLt5URUXLrwtOvuzNLiahaC1YTqVU/");
            // 返回的resp是一个GeneralBasicOCRResponse的实例，与请求对象对应
            GeneralBasicOCRResponse resp = client.GeneralBasicOCR(req);
            // 输出json格式的字符串回包
            System.out.println(GeneralBasicOCRResponse.toJsonString(resp));
            // {"TextDetections":[{"DetectedText":"<->","Confidence":100,"Polygon":[{"X":2098,"Y":244},{"X":2757,"Y":265},{"X":2746,"Y":619},{"X":2087,"Y":598}],"AdvancedInfo":"{\"Parag\":{\"ParagNo\":2}}","ItemPolygon":{"X":2098,"Y":244,"Width":659,"Height":354},"Words":[],"WordCoordPoint":[]},{"DetectedText":"OCR文字识别的应用","Confidence":100,"Polygon":[{"X":467,"Y":665},{"X":1655,"Y":665},{"X":1655,"Y":798},{"X":467,"Y":798}],"AdvancedInfo":"{\"Parag\":{\"ParagNo\":1}}","ItemPolygon":{"X":467,"Y":665,"Width":1188,"Height":133},"Words":[],"WordCoordPoint":[]},{"DetectedText":"OCR","Confidence":100,"Polygon":[{"X":2093,"Y":750},{"X":2751,"Y":750},{"X":2751,"Y":1015},{"X":2093,"Y":1015}],"AdvancedInfo":"{\"Parag\":{\"ParagNo\":3}}","ItemPolygon":{"X":2093,"Y":750,"Width":658,"Height":265},"Words":[],"WordCoordPoint":[]},{"DetectedText":"及背后技术原理","Confidence":100,"Polygon":[{"X":463,"Y":894},{"X":1375,"Y":894},{"X":1375,"Y":1026},{"X":463,"Y":1026}],"AdvancedInfo":"{\"Parag\":{\"ParagNo\":1}}","ItemPolygon":{"X":463,"Y":894,"Width":912,"Height":132},"Words":[],"WordCoordPoint":[]}],"Language":"zh","Angel":359.99,"PdfPageSize":0,"Angle":359.99,"RequestId":"35a25be4-dcb7-4a11-b8a8-5501fc2cb468"}
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
}
